﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the API Concepts Framework
 * http://apiconceptsframework.codeplex.com
 *
 * Copyright (C) 2014 eva Kühn, Thomas Scheller, Vienna University of Technology
 * http://www.complang.tuwien.ac.at/eva
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using ApiConceptsFramework.HCs;

namespace ApiConceptsFramework.FluentApi
{
	/// <summary>
	/// Extension methods for the high-level concept xml.
	/// </summary>
	public static class XmlExtensions
	{
		/// <summary>
		/// Defines that an XML configuration file is used.
		/// </summary>
		/// <param name="eval">The ApiEvaluator instance.</param>
		/// <param name="filename">The name of the XML file (just for information, not used for any kind of calculation).</param>
		/// <param name="schemaAvailable">Defines whether an XML schema is available in the IDE, to enable auto completion features. This significantly influences the complexity of XML elements and attributes.</param>
		public static void XmlConfigurationFile(this ApiEvaluator eval, string filename, bool schemaAvailable)
		{
			var data = eval.GetOrCreateHcInputData<XmlInputData>();
			data.ConfigFileName = filename;
			data.SchemaAvailable = schemaAvailable;
		}

		/// <summary>
		/// Defines an XML element that is used in the XML configuration.
		/// </summary>
		/// <param name="eval">The ApiEvaluator instance.</param>
		/// <param name="elementName">The name of the XML element.</param>
		/// <param name="numberOfUsages">The number of usages (default = 1).</param>
		/// <param name="previousUsages">The number of previous usages (default = 0), used for calculating the learning effect.</param>
		/// <param name="needsLookupOfExternalInformation">Defines if lookup of external information is required. This could for example be for the path of a file or the name of a class or method.</param>
		/// <returns>The XmlElement for further configuration.</returns>
		public static XmlElement XmlElement(this ApiEvaluator eval, string elementName, int numberOfUsages = 1, int previousUsages = 0, bool needsLookupOfExternalInformation = false)
		{
			var data = eval.GetOrCreateHcInputData<XmlInputData>();
			var element = new XmlElement
				{
					Name = elementName,
					NumberOfUsages = numberOfUsages,
					PreviousUsages = previousUsages,
					NeedsLookupOfExternalInformation = needsLookupOfExternalInformation
				};
			data.Elements.Add(element);
			return element;
		}

		/// <summary>
		/// Defines an XML attribute that is part of the XML element.
		/// </summary>
		/// <param name="element">The XML element.</param>
		/// <param name="attributeName">The name of the attribute.</param>
		/// <param name="numberOfUsages">The number of usages (default = 1).</param>
		/// <param name="previousUsages">The number of previous usages (default = 0), used for calculating the learning effect.</param>
		/// <param name="needsLookupOfExternalInformation">Defines if lookup of external information is required. This could for example be for the path of a file or the name of a class or method.</param>
		/// <returns>The XmlElement for further configuration.</returns>
		public static XmlElement XmlAttribute(this XmlElement element, string attributeName, int numberOfUsages = 1, int previousUsages = 0, bool needsLookupOfExternalInformation = false)
		{
			element.Attributes.Add(new XmlAttribute
				{
					Name = attributeName,
					NumberOfUsages = numberOfUsages,
					PreviousUsages = previousUsages,
					NeedsLookupOfExternalInformation = needsLookupOfExternalInformation
				});
			return element;
		}
	}
}
